package com.zgy.myshellcommand.handler.impl;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zgy.myshellcommand.anno.CommandLogicHandler;
import com.zgy.myshellcommand.capabilities.WriteToExcel;
import com.zgy.myshellcommand.commandentity.FileToExcelEntity;
import com.zgy.myshellcommand.commandentity.MultiFileToExcelEntity;
import com.zgy.myshellcommand.commandentity.SheetConfigEntity;
import com.zgy.myshellcommand.enums.F2eOptionsEnum;
import com.zgy.myshellcommand.handler.FileToExcelHandler;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.shell.command.CommandContext;

import java.nio.charset.Charset;
import java.nio.file.Paths;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.stream.Collectors;

@CommandLogicHandler
public class FileToExcelHandlerImpl implements FileToExcelHandler {

    @Autowired
    private FileToExcelEntity fileToExcelEntity;

    @Autowired
    private SheetConfigEntity sheetConfigEntity;

    @Resource(name = "writeToExcelImplByPoi")
//    @Resource(name = "writeToExcelImplByEasyToExcel")
    private WriteToExcel writeToExcel;

//    @Autowired
//    private Jobs jobs;
    @Autowired
    private ExecutorService executorService;

    @Override
    public void f2eHandle(CommandContext ctx) {
        System.out.println("包含两个子命令:");
        System.out.printf("[%s]:%s%n\t使用 %s %s --help 获取帮助%n", F2eOptionsEnum.Cmd.getShortName(),F2eOptionsEnum.Cmd.getDescription(),F2eOptionsEnum.Command.getLongName(),F2eOptionsEnum.Cmd.getShortName());
        System.out.printf("[%s]:%s%n\t使用 %s %s --help 获取帮助%n",F2eOptionsEnum.file.getShortName(),F2eOptionsEnum.file.getDescription(),F2eOptionsEnum.Command.getLongName(),F2eOptionsEnum.file.getShortName());
    }

    @Override
    public String cmdHandle(CommandContext ctx) {

        if(ctx.hasMappedOption(F2eOptionsEnum.CmdEx.getLongName())){
            System.out.println(F2eOptionsEnum.CmdEx.getDefaultValue());
            return "执行成功";
        }

        fileToExcelEntity.setModuleExcel(ctx.getOptionValue(F2eOptionsEnum.ModuleExcel.getLongName()));
        fileToExcelEntity.setInsertExcel(ctx.getOptionValue(F2eOptionsEnum.InsertExcel.getLongName()));
        fileToExcelEntity.setCommitRow(ctx.getOptionValue(F2eOptionsEnum.CommitRow.getLongName()));
        sheetConfigEntity.setSheetName(ctx.getOptionValue(F2eOptionsEnum.SheetName.getLongName()));
        sheetConfigEntity.setDataFileName(ctx.getOptionValue(F2eOptionsEnum.DataFileName.getLongName()));
        sheetConfigEntity.setDataFileFieldSeparator(ctx.getOptionValue(F2eOptionsEnum.DataFileFieldSeparator.getLongName()));
        sheetConfigEntity.setDataFileCharSet(ctx.getOptionValue(F2eOptionsEnum.DataFileCharSet.getLongName()));

        JSONArray objects = JSONUtil.parseArray(ctx.getOptionValue(F2eOptionsEnum.DataFileNumberTypeCols.getLongName()).toString());
        List<Integer> collect = objects.stream().map(o -> (Integer) o).collect(Collectors.toList());
        sheetConfigEntity.setDataFileNumberTypeCols(collect);

        sheetConfigEntity.setExcelStartRowNum(ctx.getOptionValue(F2eOptionsEnum.ExcelStartRowNum.getLongName()));
        sheetConfigEntity.setExcelStartColNum(ctx.getOptionValue(F2eOptionsEnum.ExcelStartColNum.getLongName()));

        writeToExcel.writeToExcel(fileToExcelEntity);
        return "执行成功";
    }

    @Override
    public String fileHandle(CommandContext ctx) {
        if(ctx.hasMappedOption(F2eOptionsEnum.FileEx.getLongName())){
            System.out.println(F2eOptionsEnum.FileEx.getDefaultValue());
            return "执行成功";
        }

        if(ctx.hasMappedOption(F2eOptionsEnum.JsonFormat.getLongName())){
            JSONObject jsonObject = JSONUtil.parseObj(F2eOptionsEnum.JsonFormat.getDefaultValue());
            System.out.println(JSONUtil.toJsonPrettyStr(jsonObject));
            return "执行成功";
        }
        String charSet=ctx.hasMappedOption(F2eOptionsEnum.JsonPathCharSet.getLongName())
            ?ctx.getOptionValue(F2eOptionsEnum.JsonPathCharSet.getLongName())
            :F2eOptionsEnum.JsonPathCharSet.getDefaultValue();
        JSONObject jsonObject = JSONUtil.readJSONObject(Paths.get(ctx.getOptionValue(F2eOptionsEnum.JsonPath.getLongName()).toString()).toFile(), Charset.forName(charSet));
        MultiFileToExcelEntity multiFileToExcelEntity = JSONUtil.toBean(jsonObject, MultiFileToExcelEntity.class);
        multiFileToExcelEntity.getFileToExcelEntities().forEach(__ -> executorService.execute(()->writeToExcel.writeToExcel(__)));
        return "执行成功";
    }
}
